
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace Articles
{
	public class ArticleCalendarSupportBizRules
	{
		/// <summary>
		/// This bizrule creates a bizobj list of AlbumUpdateDateInfo bizobjs based on the date range
		/// provided.  The AlbumUpdateDateInfo provides summaries of how many albums were updated and
		/// created on a specific day.
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <param name="dates">A fleximap that contains "firstDate" and "lastDate" entries that are
		/// DateTime type objects.</param>
		/// <returns></returns>
		public IBizObjList getArticleSetUpdateDates(IApplication app, IRuleContext context, FlexiMap dates)
		{
			DateTime firstDate = (DateTime)dates["firstDate"];
			DateTime lastDate = (DateTime)dates["lastDate"];

			IBizObjList listDateInfo = app.bizObjMgr.createList("ArticleSetUpdateDateInfo");
			IKeyedBizObjList sortedListDateInfo = listDateInfo.getIndex("date");
			IBizObjList listUpdated = app.appData.get("DA.Articles.ArticleSetUpdateDates", dates) as IBizObjList;
			if (listUpdated != null && listUpdated.numElements > 0)
			{
				IBizObj dateInfo = null;
				foreach (IBizObj bizobj in listUpdated)
				{
					DateTime createDate = (DateTime)bizobj.get("dateCreated");
					createDate = new DateTime(createDate.Year, createDate.Month, createDate.Day);
					if (createDate >= firstDate && createDate <= lastDate)
					{
						if (dateInfo == null || !dateInfo.get("date").Equals(createDate))
						{
							dateInfo = sortedListDateInfo.insertNew();
							dateInfo.put("date", createDate);
						}
						int numCreated = (int)dateInfo.get("created");
						dateInfo.put("created", ++numCreated);
					}
				}

				sortedListDateInfo.refresh();
				foreach (IBizObj bizobj in listUpdated)
				{
					DateTime updatedDate = (DateTime)bizobj.get("dateUpdated");
					updatedDate = new DateTime(updatedDate.Year, updatedDate.Month, updatedDate.Day);
					if (updatedDate >= firstDate && updatedDate <= lastDate)
					{
						dateInfo = sortedListDateInfo.find(updatedDate, false);
						if (dateInfo == null)
						{
							dateInfo = sortedListDateInfo.insertNew();
							dateInfo.put("date", updatedDate);
							sortedListDateInfo.refresh();
						}
						int numUpdated = (int)dateInfo.get("updated");
						dateInfo.put("updated", ++numUpdated);
					}
				}
			}
			return sortedListDateInfo;
		}

		public IBizObjList getArticleSetUpdateDetailsDates(IApplication app, IRuleContext context, FlexiMap dates)
		{
			DateTime firstDate = (DateTime)dates["firstDate"];
			DateTime lastDate = (DateTime)dates["lastDate"];

			IBizObjList listUpdated = app.appData.get("DA.Articles.ArticleSetUpdateDates", dates) as IBizObjList;
			IBizObjList listDateInfo = app.bizObjMgr.createList("ArticleSetUpdateDateDetailsInfo");

			if (listUpdated != null && listUpdated.numElements > 0)
			{
				DateTime date;
				IBizObj dateInfo = null;
				foreach (IBizObj bizobj in listUpdated)
				{
					date = (DateTime)bizobj.get("dateCreated");
					date = new DateTime(date.Year, date.Month, date.Day);
					if (date >= firstDate && date <= lastDate)
					{
						dateInfo = listDateInfo.insertNew();
						dateInfo.put("date", date);
						dateInfo.put("change", "created");
						dateInfo.put("name", bizobj.get("name"));
						dateInfo.put("description", bizobj.get("shortDescription"));
						dateInfo.put("setId", bizobj.get("id"));
						dateInfo.put("mediaCacheName", bizobj.get("mediaCacheName"));
						dateInfo.put("image",
							String.Format("/img/{0}/Thumbnail/{1}",
							bizobj.get("mediaCacheName"), bizobj.get("highlightPicture.name")));
						dateInfo.put("groupId", bizobj.get("groupId"));
					}
				}
				IKeyedBizObjList sortedAlbumDateInfo = listDateInfo.getIndex("albumId");

				foreach (IBizObj bizobj in listUpdated)
				{
					date = (DateTime)bizobj.get("dateUpdated");
					date = new DateTime(date.Year, date.Month, date.Day);
					if (date >= firstDate && date <= lastDate)
					{
						dateInfo = sortedAlbumDateInfo.find((IComparable)bizobj.get("id"), false);
						if (dateInfo == null || ((DateTime)dateInfo.get("date")).DayOfYear != date.DayOfYear)
						{
							dateInfo = listDateInfo.insertNew();
							dateInfo.put("date", date);
							dateInfo.put("change", "updated");
							dateInfo.put("name", bizobj.get("name"));
							dateInfo.put("description", bizobj.get("shortDescription"));
							dateInfo.put("albumId", bizobj.get("id"));
							dateInfo.put("mediaCacheName", bizobj.get("mediaCacheName"));
							dateInfo.put("image",
								String.Format("/img/{0}/Thumbnail/{1}",
								bizobj.get("mediaCacheName"), bizobj.get("highlightPicture.name")));
							dateInfo.put("articleGroupId", bizobj.get("articleGroupId"));
						}
					}
				}
			}

			return listDateInfo.getIndex("date");
		}
	}
}
